/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_admin.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.manager.dao;

import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.model.maps.LinkedMapSO;
import org.zhiqim.kernel.model.results.R2;
import org.zhiqim.kernel.model.results.RE;
import org.zhiqim.kernel.util.Classes;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 通用数据访问对象
 *
 * @version v8.0.4 @author zgzou 2022年5月14日 新建与整理
 */
@AnAlias("ZmrCommonDao")
public class ZmrCommonDao
{
    /**
     * 上下移动对象
     * 
     * @param down          是否向下移动
     * @param dbo           DBO对象
     * @param idField       ID字段
     * @param seqField      序号字段
     * @param conditions    条件列表
     * @return              R2
     */
    public static <T> R2 doMoveSeq(boolean down, T dbo, String idField, String seqField, LinkedMapSO conditionMap)
    {
        return doMoveSeq(down, dbo, idField, seqField, conditionMap, Integer.MAX_VALUE);
    }
    
    /**
     * 上下移动对象
     * 
     * @param down          是否向下移动
     * @param dbo           DBO对象
     * @param idField       ID字段
     * @param seqField      序号字段
     * @param seqMax        序号最大值
     * @param conditions    条件列表
     * @return              R2
     */
    public static <T> R2 doMoveSeq(boolean down, T dbo, String idField, String seqField, LinkedMapSO conditionMap, int seqMax)
    {
        Integer seq = (Integer)Classes.getFieldValue(dbo, seqField);
        if (down && seq >= seqMax)
        {//向下有限制最大值的
            return R2.error("该对象在最下面，不支持向下移动");
        }
        
        int seqChg = down?seq+1:seq-1;
        Selector selector = new Selector().addMust(seqField, seqChg);
        if (conditionMap != null)
        {
            for (String key : conditionMap.keySet())
            {
                selector.addMust(key, conditionMap.get(key));
            }
        }
        
        @SuppressWarnings("unchecked")
        RE<T> result = (RE<T>)ORM.tabler().item(dbo.getClass(), selector);
        if (result.failure())
        {//读取要交换的
            return R2.error(result.error());
        }
        
        if (result.value() == null)
        {//没有，则认为自己是最上面或最下面
            return R2.error("该对象在最"+(down?"下":"上")+"面，不支持向"+(down?"下":"上")+"移动");
        }
        
        Object idCur = Classes.getFieldValue(dbo, idField);
        Object idChg = Classes.getFieldValue(result.value(), idField);
        
        Updater curUpdater = new Updater().addMust(idField, idCur);
        Updater chgUpdater = new Updater().addMust(idField, idChg);
        if (conditionMap != null)
        {
            for (String key : conditionMap.keySet())
            {
                curUpdater.addMust(key, conditionMap.get(key));
                chgUpdater.addMust(key, conditionMap.get(key));
            }
        }
        
        ORM.tabler().update(dbo.getClass(), curUpdater.addFieldExpression(seqField, seqField+(down?"+":"-")+"1"));
        ORM.tabler().update(dbo.getClass(), chgUpdater.addFieldExpression(seqField, seqField+(down?"-":"+")+"1"));
        
        return R2.correct();
    }
}
